﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Interactivity;
using System.Reflection;
using System.Globalization;
//using HugeFlow.Common;

namespace HugeFlow.CommandPattern.Interactivity
{
    /// <summary>
    /// RoutedEventHandler_T_ 타입의 Value를 Parameter로 넘겨주는 EventTrigger
    /// EventArgs에 Value 나 NewValue라는 Property 속성이 있으면 Parameter로 넘겨줌.
    /// </summary>
    public class RoutedEventParamTrigger : System.Windows.Interactivity.EventTrigger
    {
        /// <summary>
        ///     Called when the event associated with this EventTriggerBase is fired. By
        ///     default, this will invoke all actions on the trigger.        
        /// </summary>
        /// <param name="eventArgs"> The System.EventArgs instance containing the event data.</param>
        protected override void OnEvent(EventArgs eventArgs)
        {
            PropertyInfo pi = eventArgs.GetType().GetProperty("Value");

            if (pi == null)
            {
                pi = eventArgs.GetType().GetProperty("NewValue");
            }

            if (pi != null)
            {
                foreach (System.Windows.Interactivity.TriggerAction ta in this.Actions)
                {
                    InvokeCommandAction ica = ta as InvokeCommandAction;
                    if (ica != null)
                    {
                        ica.CommandParameter = pi.GetValue(eventArgs, null);
                        break;
                    }
                }
            }

            base.OnEvent(eventArgs);
        }
    }
}
